<?php

namespace App\Console\Stats\Settlement;

use App\Console\Stats\Command;
use App\Exceptions\Response\ServiceException;
use Helper\Timed;

class Bulk extends Command
{

    /**
     * 控制台命令 signature 的名称。
     *
     * @var string
     */
    protected $signature = 'stats:settlement:bulk {timed?} {timedEnd?}';

    /**
     * 控制台命令说明。
     *
     * @var string
     */
    protected $description = '【结算统计】【批量】创建统计任务，并发布到队列';

    /**
     * 执行控制台命令。
     *
     * @return mixed
     */
    public function handle()
    {
        // 开始时间
        $timed = $this->getTimed();
        // 结束时间
        $timedEnd = $this->argument('timedEnd') ?: time();
        // 输出提示【任务时间】
        $this->info($this->description . date('Y-m-d H:i:s', $timed) . '~' . date('Y-m-d H:i:s', $timedEnd));

        $timedDay = Timed::getDayCurrentTimed($timed);
        $timedDayCurrent = Timed::getDayCurrentTimed($timedEnd);
        // 创建统计任务
        for (; $timedDay < $timedDayCurrent; $timedDay += 86400) {
            try {
                $this->call('stats:settlement:all:task:day', [
                    'timed' => $timedDay
                ]);
            } catch (ServiceException $e) {
            }
            try {
                $this->call('stats:settlement:game:task:day', [
                    'timed' => $timedDay
                ]);
            } catch (ServiceException $e) {
            }
            try {
                $this->call('stats:settlement:game:scene:task:day', [
                    'timed' => $timedDay
                ]);
            } catch (ServiceException $e) {
            }
        }
        // 发布任务到队列
        $this->call('stats:settlement:all:job:day');
        $this->call('stats:settlement:game:job:day');
        $this->call('stats:settlement:game:scene:job:day');
    }
}
